package cn.itsub.demo.game;

import cn.itsub.sodo.Sprite;
import cn.itsub.sodo.action.base.Action;
import cn.itsub.sodo.action.Animate;
import cn.itsub.sodo.action.base.RepeatForever;
import cn.itsub.sodo.action.interval.MoveBy;
import cn.itsub.sodo.audio.SoundEngine;
import cn.itsub.sodo.cache.AnimateCache;
import cn.itsub.sodo.timer.HiScheduler;
import cn.itsub.sodo.timer.UpdateCallback;

public class Hero extends Sprite {
	private double hp;
	private double hpmax;
	public int dir=2;//面向方向：1左2右
	public String state = "idle";
	public float speed = 335;
	
	public Hero() {
		super("shana/idle/idle1.png");
		init();
	}
	
	private void init(){
		Animate ani = null;
		//空闲动作
		if(AnimateCache.get("shana-idle")==null){
			ani = Animate.create(0.8f);
			for(int i=1;i<=4;i++){
				ani.addFrame("shana/idle/idle"+i+".png");
			}
			AnimateCache.put("shana-idle", ani);
		}
		
		//奔跑动作
		if(AnimateCache.get("shana-run")==null){
			ani = Animate.create(0.8f);
			for(int i=1;i<=8;i++){
				ani.addFrame("shana/run/run"+i+".png");
			}
			AnimateCache.put("shana-run", ani);
		}
		
		
		//攻击动作1
		if(AnimateCache.get("shana-atk1")==null){
			ani = Animate.create(0.7f);
			for(int i=1;i<=7;i++){
				ani.addFrame("shana/atk1/00"+i+".png");
			}
			AnimateCache.put("shana-atk1", ani);
		}
		
		//攻击动作2
		if(AnimateCache.get("shana-atk2")==null){
			ani = Animate.create(0.9f);
			for(int i=1;i<=9;i++){
				ani.addFrame("shana/atk2/00"+i+".png");
			}
			AnimateCache.put("shana-atk2", ani);
		}

		//注册update更新
		HiScheduler.getInstance().scheduleUpdate(this,0,false);
		
	}

	public void update(float t) {
		//System.out.println(t);
		if ("run".equals(state)) {
			if (dir == 2) {
				position.x+=speed*t;
				flipY = true;
			}
			if (dir == 1) {
				position.x-=speed*t;
				flipY = false;
			}
		}else{
			if (dir == 2) {
				flipY = false;
			}
			if (dir == 1) {
				flipY = true;
			}
		}

	}
	
	public void idle(){
		state = "idle";
		Animate ani = AnimateCache.get("shana-idle");
		Action act = RepeatForever.create(ani.copy());
		this.stopAllAction();
		this.runAction(act);
	}
	public void attack(){
		Animate ani = null;
		if(Math.random()>0.5){
			state = "atk1";
			ani = AnimateCache.get("shana-atk1");
		}else{
			state = "atk2";
			ani = AnimateCache.get("shana-atk2");
		}
		
		//Action act = Repeat.create(ani.copy(), 1);
		this.stopAllAction();
		this.runAction(ani.copy());

		SoundEngine.getInstance().playEffect("music/swish-1.mp3", false, 50);
		
		
	}
	public void run(){
		if ("run".equals(state)) {
			return;
		}
		state = "run";
		Animate ani = AnimateCache.get("shana-run");
		Action act = RepeatForever.create(ani.copy());
		this.stopAllAction();
		this.runAction(act);
	}
	public void stop(){
		if ("stop".equals(state)) {
			return;
		}
		state = "stop";
		this.stopAllAction();
	}
	
}
